package com.fuershiye.woman.monitor.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.fuershiye.woman.monitor.beans.*;
import com.fuershiye.woman.monitor.constants.Massage;
import org.apache.shiro.SecurityUtils;
//import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fuershiye.woman.monitor.model.Department;
import com.fuershiye.woman.monitor.service.AreaService;
import com.fuershiye.woman.monitor.service.BridgeService;
import com.fuershiye.woman.monitor.service.DepartmentService;
import com.fuershiye.woman.monitor.service.DepartmentTypeService;
import com.fuershiye.woman.monitor.utils.MyUtilString;
import com.google.common.collect.Maps;

/**
 * 部门的管理
 * 
 * @author yongyongdong
 *
 */
@Controller
@RequestMapping(value = "/department")
public class DepartmentController {

	private static final Logger logger = LoggerFactory
			.getLogger(DepartmentController.class);

	@Autowired
	DepartmentService departmentService;
	@Autowired
	DepartmentTypeService departmentTypeService;
	@Autowired
	AreaService areaService;
	@Autowired
	BridgeService bridgeService;

//	@RequiresPermissions("department:view")
	@RequestMapping(value = "/view.do")
	public String view(String page, Model model) {
		
		List<IdNamePid> departList = departmentTypeService.getAllNames();
    	model.addAttribute("departList", departList);
    	
    	List<IdNameSname> pidDepartList = departmentService.getAllSnames(null,null,null);
    	model.addAttribute("pidDepartList", pidDepartList);
    	
    	List<IdNamePid> areaList = areaService.getAllNames();
    	model.addAttribute("areaList", areaList);
    	
		return "department/" + page;
	}

//	@RequiresPermissions("department:list")
	@RequestMapping(value = "/list.do")
	@ResponseBody
	public Map<String, Object> getPage(String name, Long type, Integer level, Integer status, Integer page,
			Integer pageSize) {
		
		Page pageObj = new Page(page, pageSize);
		
		Map<String, Object> result = Maps.newHashMap();
		
		result.put("rows", departmentService.list(name, type, level, status, pageObj));
		result.put("total", departmentService.getTotal(name, type, level, status));
		result.put("departmentTypes", departmentTypeService.getAllNames());
		result.put("departmentes", departmentService.getAllSnames(null,null,null));
    	result.put("areaList", areaService.getAllNames());
		
		return result;
		
	}
	
//	@RequiresPermissions("department:samePid")
	@RequestMapping(value = "/samePid.do")
	@ResponseBody
	public List<IdNameArea> getSamePid(Long pid) {
		
		return departmentService.listByPidOrArea(pid, null);
	}

//	@RequiresPermissions("department:myDepart")
	@RequestMapping(value = "/myDepart.do")
	@ResponseBody
	public Department getMyDepart() {
		return bridgeService.getUserDepartment();
	}

//	@RequiresPermissions("department:sameArea")
	@RequestMapping(value = "/sameArea.do")
	@ResponseBody
	public List<IdNameArea> getSameArea(Long area) {
		
		return departmentService.listByPidOrArea(null, area);
	}

	//	@RequiresPermissions("department:sameArea")
	@RequestMapping(value = "/levelAndArea.do")
	@ResponseBody
	public Result getSameArea(Integer level, Long areaid) {

		if(level==null||level==0){
			return new Result(Massage.ERROR_PARAMETER);
		}
		if(areaid==null||areaid==0){
			areaid=null;
		}
		List<IdNameSname> result = departmentService.getAllByLevelAndArea(level, areaid, 1);
		return new Result(Massage.SUCCESS, new PageResult(result, result.size()));
	}

//	@RequiresPermissions("department:save")
	@RequestMapping(value = "/save.do")
	@ResponseBody
	public boolean save(Long id, HttpServletRequest request) {
		Department department = null;
		boolean first = false;
		if (id == null || id == 0) {
			department = new Department();
			first = true;
		} else {
			department = departmentService.get(id);
			if (department == null) {
				logger.error("FormsController update,FormsController is null");
				return false;
			}
		}

		Department saveDepartment = getPostValue(first, department, request);
		departmentService.save(saveDepartment);
		return true;
	}

//	@RequiresPermissions("department:delete")
	@RequestMapping(value = "/delete.do")
	@ResponseBody
	public boolean delete(Long id) {
		Department department = departmentService.get(id);
		department.setStatus(0); // 修改为已删除
		departmentService.save(department);
		return true;
	}

	private Department getPostValue(boolean first, Department department,
			HttpServletRequest request) {

		String code = request.getParameter("code");
		String name = request.getParameter("name");
		String sname = request.getParameter("sname");
		String type = request.getParameter("type");
		String level = request.getParameter("level");
		String pid = request.getParameter("pid");
		String area = request.getParameter("area");
		String charge = request.getParameter("charge");
		String author = request.getParameter("author");
		String phone = request.getParameter("phone");
		String contact = request.getParameter("contact");
		String email = request.getParameter("email");
		String address = request.getParameter("address");

		if (!MyUtilString.isBlankNullZero(code)) {
			department.setCode(code);
		}
		if (!MyUtilString.isBlankNullZero(name)) {
			department.setName(name);
		}
		if (!MyUtilString.isBlankNullZero(sname)) {
			department.setSname(sname);
		}
		if (!MyUtilString.isBlankNull(type)) {
			department.setType(Long.valueOf(type));
		}
		if (!MyUtilString.isBlankNull(pid)) {
			department.setPid(Long.valueOf(pid));
		}
		if (!MyUtilString.isBlankNull(area)) {
			department.setArea(Long.valueOf(area));
		}
		if (!MyUtilString.isBlankNull(level)) {
			department.setLevel(Integer.valueOf(level));
		}
		if (!MyUtilString.isBlankNullZero(charge)) {
			department.setCharge(charge);
		}
		if (!MyUtilString.isBlankNullZero(author)) {
			department.setAuthor(author);
		}
		if (!MyUtilString.isBlankNullZero(phone)) {
			department.setPhone(phone);
		}
		if (!MyUtilString.isBlankNullZero(contact)) {
			department.setContact(contact);
		}
		if (!MyUtilString.isBlankNullZero(email)) {
			department.setEmail(email);
		}
		if (!MyUtilString.isBlankNullZero(address)) {
			department.setAddress(address);
		}
		
		if(first){
			department.setCtime(new Date());
		}
		department.setEditor(SecurityUtils.getSubject().getPrincipal()+"");
		department.setMtime(new Date());
		department.setStatus(1);

		return department;
	}

}
